Java Programming Language
Java Array Exercises

These exercises are designed to evaluate your knowledge, comprehension, application and analysis of Java Arrays. It is expected that you should be able to correctly answer all or nearly all of the questions and problems presented. You should do these exercises by yourself.

1. Could you have a single array that has the integer 3, the String "Gator" and the double 453.238 in it? Why or Why not?

 

 

2. Indicate whether the following are valid syntax of Java arrays:

Java Statement

Valid or Invalid (why invalid)

int[] a1;

 

char c2[] = new char[10];

 

String[] s3 = new String[];

 

double d4[];

 

int a2[100] = new int[];

 

a3[] = new int[10];

 

double d1[] = new double[12.5];

 

int i1[] = {1,2,3,4,5};

 

String zzz = new String[4];

 

int i = i1[5];

 

Char c = c2[0];

 

Zzz[3] = "Hello";

 

c2[3] = ‘x’;

 

a1 = new int[25];

 

System.out.println(zzz[3]);

 

 

3. Write the Java code to declare an integer array of 50 slots and assign the value of the index number of a slot * 3 to each slot. Then write the code to print the 5th, 12th and 48th element of the array.

 

 

 

 

4. Given: int[] a = new int[100];

What do each of the following statements do?

  1. System.out.println(a.length());
  2.  

  3. if (a.length < 1000) System.out.println("small array");

 

5. What is the difference between "shallow" and "deep" array copying?

 

 

6. Write the Java code to declare, create and initialize a 3 dimensional float array with the products (multiplication) of the 3 index values.

 

 

7. Why is an array a composite data type and why is the array variable considered a reference to the array values?